package org.flik.server.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;


//@NamedQueries({ @NamedQuery(name = Ticket.FIND_TICKET, query = "select t from Ticket t where t.project.id=:"
//		+ QueryParams.PROJECCT_ID)})

@NamedQueries({
	@NamedQuery(name=Ticket.QUERY_FIND_TICKET, query = "select t from Ticket t inner join fetch t.reportedBy inner join fetch t.assignee inner join fetch t.project inner join fetch t.ticketType inner join fetch t.ticketStatus where t.project.id=:" + QueryParams.PROJECT_ID + " order by t.createDate desc"), 
	@NamedQuery(name=Ticket.QUERY_TICKET_COUNT, query = "select count(t) from Ticket t where t.project.id=:" + QueryParams.PROJECT_ID), 
//	@NamedQuery(name=Ticket.QUERY_TICKET_DETAIL, query = "select t from Ticket t inner join fetch t.ticketComment inner join fetch t.ticketComment.replies where t.id=:" + QueryParams.TICKET_ID)
//	@NamedQuery(name=Ticket.QUERY_TICKET_DETAIL, query = "select t from Ticket t inner join fetch t.ticketComment where t.id=:" + QueryParams.TICKET_ID)
//	@NamedQuery(name=Ticket.QUERY_TICKET_DETAIL, query = "select t from Ticket t inner join fetch t.ticketComment inner join fetch t.ticketComment.ticket inner join fetch t.ticketComment.replyTo inner join fetch t.ticketComment.replies where t.ticketComment.ticket.id=:TICKET_ID and t.id=:" + QueryParams.TICKET_ID)

//	@NamedQuery(name=Ticket.QUERY_TICKET_DETAIL, query = "select t from Ticket t inner join fetch t.project inner join fetch t.ticketStatus inner join fetch t.ticketType inner join fetch t.ticketComment where t.id=:" + QueryParams.TICKET_ID)
	@NamedQuery(name=Ticket.QUERY_TICKET_DETAIL, query = "select t from Ticket t inner join fetch t.replies inner join fetch t.ticketComment inner join fetch t.assignee inner join fetch t.reportedBy inner join fetch t.project inner join fetch t.ticketStatus inner join fetch t.ticketType inner join fetch t.ticketComment where t.id=:" + QueryParams.TICKET_ID)
})

@SuppressWarnings("serial")
@Entity
public class Ticket implements Serializable{

	public static final String QUERY_FIND_TICKET = "QUERY_FIND_TICKET";
	public static final String QUERY_TICKET_COUNT = "QUERY_TICKET_COUNT";
	public static final String QUERY_TICKET_DETAIL = "QUERY_TICKET_DETAIL";
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id; 
	
	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private FlikUser reportedBy; 

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private TicketType ticketType;
	
	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private TicketStatus ticketStatus; 

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private FlikUser assignee; 
	
	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
	private Project project;
	
	private String title;
	
	private Date createDate; 

	@OneToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
//	@JoinColumn(name="ticketComment", nullable=false)
	private TicketComment ticketComment;
	
	@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL, mappedBy="ticket")
	private Set<TicketComment> replies; 
	
	
	public TicketComment getTicketComment() {
		return ticketComment;
	}

	public void setTicketComment(TicketComment ticketComment) {
		this.ticketComment = ticketComment;
	}

	public Ticket() {
		// TODO Auto-generated constructor stub
	}
	
	public Ticket(FlikUser reportedBy, TicketType ticketType,
			TicketStatus ticketStatus, FlikUser asignee, Project project,
			String title, TicketComment ticketComment) {
		super();
		this.reportedBy = reportedBy;
		this.ticketType = ticketType;
		this.ticketStatus = ticketStatus;
		this.assignee = asignee;
		this.project = project;
		this.title = title;
		this.ticketComment = ticketComment; 
		this.createDate = new Date();
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public FlikUser getReportedBy() {
		return reportedBy;
	}

	public void setReportedBy(FlikUser reportedBy) {
		this.reportedBy = reportedBy;
	}

	public TicketType getTicketType() {
		return ticketType;
	}

	public void setTicketType(TicketType ticketType) {
		this.ticketType = ticketType;
	}

	public TicketStatus getTicketStatus() {
		return ticketStatus;
	}

	public void setTicketStatus(TicketStatus ticketStatus) {
		this.ticketStatus = ticketStatus;
	}

	public FlikUser getAssignee() {
		return assignee;
	}

	public void setAssignee(FlikUser assignee) {
		this.assignee = assignee;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Set<TicketComment> getReplies() {
		return replies;
	}

	public void setReplies(Set<TicketComment> replies) {
		this.replies = replies;
	}

}
